/*
	Copyright (c) 2009, M.P. Bartnig & A. Gosman
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
package nl.sanity.ui.helpers
{
	import flash.errors.IllegalOperationError;
	import flash.text.StyleSheet;
	import flash.text.TextFormat;
	
	/**
	 * LabelStyle
	 *
	 * @langversion ActionScript 3
	 *
	 * @author M.P. Bartnig
	 * @since  16/03/2009
	 */
	public class LabelStyle extends StyleSheet
	{
		protected static var FONT_FAMILY 				:String		= "Verdana";
		protected static var FONT_SIZE					:uint		= 12;
		protected static var FONT_WEIGHT				:String		= "";
		protected static var COLOR						:String		= "#000000";
		protected static var TEXT_ALIGN					:String		= "center";
		protected static var TEXT_DECORATION			:String		= "none";
		
		// CSS definitions
		public static const	CSS_UP						:String		= ".up";
		public static const	CSS_OVER					:String		= ".over";
		public static const	CSS_DOWN					:String		= ".down";
		public static const	CSS_DISABLED				:String		= ".disabled";
		
		public static const	CSS_SELECTED_UP				:String		= ".selectedUp";
		public static const	CSS_SELECTED_OVER			:String		= ".selectedOver";
		public static const	CSS_SELECTED_DOWN			:String		= ".selectedDown";
		public static const	CSS_SELECTED_DISABLED		:String		= ".selectedDisabled";
		
		public static const	CSS_ENABLED					:String		= ".enabled";
		
		public static const	CSS_BODY					:String		= "body";
		public static const	CSS_H1						:String		= "h1";
		public static const	CSS_PARAGRAPH				:String		= "p";
		public static const	CSS_ANCHOR					:String		= "a";
		public static const	CSS_ANCHOR_HOVER			:String		= "a:hover";
		public static const	CSS_ANCHOR_VISITED			:String		= "a:visited";
		
		// Style objects
		public static const	DEFAULT_STYLE				:Object		= { 	fontFamily: FONT_FAMILY, 
																			fontSize: FONT_SIZE,
																			fontWeight: FONT_WEIGHT,
																			color: COLOR,
																			textAlign: TEXT_ALIGN,
																			textDecoration: TEXT_DECORATION };
		
		// Button related																	
		public static const	OVER						:Object		= DEFAULT_STYLE;
		public static const	DOWN						:Object		= DEFAULT_STYLE;
		public static const	DISABLED					:Object		= DEFAULT_STYLE;
		
		public static const	SELECTED_UP					:Object		= DEFAULT_STYLE;
		public static const	SELECTED_OVER				:Object		= DEFAULT_STYLE;
		public static const	SELECTED_DOWN				:Object		= DEFAULT_STYLE;
		public static const	SELECTED_DISABLED			:Object		= DEFAULT_STYLE;
		
		// General
		public static const	ENABLED						:Object		= DEFAULT_STYLE;
		
		public static const	BODY						:Object		= DEFAULT_STYLE;
		public static const	H1							:Object		= DEFAULT_STYLE;
		public static const	PARAGRAPH					:Object		= DEFAULT_STYLE;
		public static const	ANCHOR						:Object		= DEFAULT_STYLE;
		public static const	ANCHOR_HOVER				:Object		= DEFAULT_STYLE;
		public static const	ANCHOR_VISITED				:Object		= DEFAULT_STYLE;
		
		// --- CONSTRUCTOR -----------------------------------------

		public function LabelStyle()
		{
			setDefaultStyles();
		}
		
		// --- PUBLIC FUNCTIONS -----------------------------------------

		public static function setDefaultFontFamily( fontFamily:String ):void
		{
			FONT_FAMILY = fontFamily;
		}
		
		public static function setDefaultFontSize( size:uint ):void
		{
			FONT_SIZE = size;
		}
		
		public static function setFontWeight( fontWeight:String ):void
		{
			FONT_WEIGHT = fontWeight;
		}
		
		public static function setDefaultColor( color:String ):void
		{
			COLOR = color;
		}
		
		public static function setTextAlign( textAlign:String ):void
		{
			TEXT_ALIGN = textAlign;
		}
		
		public static function setTextDecoration( textDecoration:String ):void
		{
			TEXT_DECORATION = textDecoration;
		}

		public function setAllStyles( styleObject:Object ):void
		{
			for ( var i:uint = 0; i < styleNames.length; i++ )
			{
				setStyle( styleNames[ i ], styleObject );
			}
		}
		
		public function getTextFormat( styleName:String ):TextFormat
		{
			if ( getStyle( styleName ) == null )
			{
				throw new IllegalOperationError ( "Unsupported styleName: " + styleName );
			}
			
			var formatObject:Object = super.getStyle( styleName );
			
			return super.transform( formatObject );
		}
		
		// --- PRIVATE FUNCTIONS -----------------------------------------

		private function setDefaultStyles():void
		{
			super.setStyle( LabelStyle.CSS_UP, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_OVER, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_DOWN, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_DISABLED, LabelStyle.DEFAULT_STYLE );
			
			super.setStyle( LabelStyle.CSS_SELECTED_UP, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_SELECTED_OVER, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_SELECTED_DOWN, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_SELECTED_DISABLED, LabelStyle.DEFAULT_STYLE );
			
			super.setStyle( LabelStyle.CSS_ENABLED, LabelStyle.DEFAULT_STYLE );
			
			super.setStyle( LabelStyle.CSS_BODY, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_H1, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_PARAGRAPH, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_ANCHOR, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_ANCHOR_HOVER, LabelStyle.DEFAULT_STYLE );
			super.setStyle( LabelStyle.CSS_ANCHOR_VISITED, LabelStyle.DEFAULT_STYLE );
		}
		
		// --- OVERRIDE FUNCTIONS -----------------------------------------
		
		override public function setStyle( styleName:String, styleObject:Object ):void
		{
			if ( getStyle( styleName ) == null )
			{
				throw new IllegalOperationError ( "Unsupported styleName: " + styleName );
			}
			
			switch ( styleName )
			{
				// Button styles
				case LabelStyle.CSS_UP:
					super.setStyle ( LabelStyle.CSS_UP, styleObject );
					super.setStyle ( LabelStyle.CSS_SELECTED_UP, styleObject );
					break;
					
				case LabelStyle.CSS_OVER:
					super.setStyle ( LabelStyle.CSS_OVER, styleObject );
					super.setStyle ( LabelStyle.CSS_SELECTED_OVER, styleObject );
					break;
					
				case LabelStyle.CSS_DOWN:
					super.setStyle ( LabelStyle.CSS_DOWN, styleObject );
					super.setStyle ( LabelStyle.CSS_SELECTED_DOWN, styleObject );
					break;
					
				case LabelStyle.CSS_DISABLED:
					super.setStyle( LabelStyle.CSS_DISABLED, styleObject );
					super.setStyle( LabelStyle.CSS_SELECTED_DISABLED, styleObject );
					break;
					
				case LabelStyle.CSS_ENABLED:
					super.setStyle( LabelStyle.CSS_ENABLED, styleObject );
					break;
					
				default:
					super.setStyle( styleName, styleObject );
					break;
			}
		}
	}
}